import pandas as pd
from pandas.core.tools.datetimes import Scalar
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

df = pd.read_csv('./scenic_data.csv')

#

features = df[['non_weekend_ratio', 'out_province_ratio', 'elderly_ratio']]
scaler = StandardScaler()
scaled_features = scaler.fit_transform(features)

from sklearn.metrics import silhouette_score
scores = []
for k in range(2, 6):
    kmeans = KMeans(n_clusters=k, random_state=42)
    labels = kmeans.fit_predict(scaled_features)
    scores.append(silhouette_score(scaled_features, labels))
plt.plot(range(2, 6), scores)
plt.xlabel("簇数量")
plt.ylabel("轮廓系数")
plt.show()

#

kmeans = KMeans(n_clusters=4, random_state=42)
df['cluster'] = kmeans.fit_predict(scaled_features)

print(df[['tourist agencv_name', 'cluster']])